package com.dhms.vostok1.model

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.dhms.vostok1.data.ApiResult
import com.dhms.vostok1.data.ApiState
import com.dhms.vostok1.data.message_center.MessageCenterData
import com.dhms.vostok1.data.message_center.ReceiveMessageData
import com.dhms.vostok1.service.net.NetworkApi
import kotlinx.coroutines.launch

class MessageCenterViewModel : ViewModel() {

    private val _messageCount = MutableLiveData(0)
    val messageCount: LiveData<Int> = _messageCount

    //展示到界面的消息类型
    private val _messageDataSource = MutableLiveData<List<MessageCenterData>>()
    val messageDataSource: LiveData<List<MessageCenterData>> = _messageDataSource

    //全部的消息类型
    private var _allMessageSource: List<MessageCenterData>? = null

    private val _receivedMessageData = MutableLiveData<ReceiveMessageData?>()
    val receivedMessageData: LiveData<ReceiveMessageData?> = _receivedMessageData

    private val _getMessageCenterListResult = MutableLiveData<ApiResult?>()
    val getMessageCenterListResult: LiveData<ApiResult?> = _getMessageCenterListResult

    fun getMessages() {
        viewModelScope.launch {
            val res = NetworkApi.getMessageUnReadCount()
            val error = res.exceptionOrNull()
            if (error != null) {
                return@launch
            }
            _messageCount.value = res.getOrNull()?.body()?.data?.total ?: 0
        }
    }

    fun getMessageCenterList() {
        viewModelScope.launch {
            val res = NetworkApi.getMessageCenterUnread()
            val error = res.exceptionOrNull()
            if (error != null) {
                _getMessageCenterListResult.value =
                    ApiResult(ApiState.ERROR, error.message ?: "获取失败！")
                return@launch
            }
            val data = res.getOrNull()?.body()?.data?.items
            _allMessageSource = data
            updateMessageDataSource()
            _getMessageCenterListResult.value = ApiResult(ApiState.DONE)
        }
    }

    fun receiveMessage(data: ReceiveMessageData) {
        data.group?.let { messageGroup ->
            viewModelScope.launch {
                _messageCount.value = (_messageCount.value ?: 0) + 1
            }
            _allMessageSource?.firstOrNull { messageGroup.value == it.messageGroup }
                ?.let { messageCenterData ->
                    messageCenterData.lastTitle = data.title
                    messageCenterData.lastTime = data.messageTime
                    messageCenterData.unreadCount =
                        (messageCenterData.unreadCount ?: 0) + 1

                    Log.i(TAG, "messages: $_allMessageSource")
                    viewModelScope.launch {
                        _receivedMessageData.value = data
                        updateMessageDataSource()
                    }
                }
        }
    }

    private fun updateMessageDataSource() {
        _messageDataSource.value = _allMessageSource?.filter { !it.lastTitle.isNullOrEmpty() }
            ?.sortedByDescending { it.lastTime }
    }

    companion object {
        const val TAG = "MessageCenterViewModel"
    }
}

